package cn.keniykey.mc.landterritory.landview;

import java.util.Collections;
import java.util.List;

import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import cn.keniykey.mc.landterritory.config.LanguageManager;
import cn.keniykey.mc.landterritory.config.ViewsConfiguration;
import cn.keniykey.mc.landterritory.config.ViewsConfiguration.ViewConfig;
import cn.keniykey.mc.landterritory.land.api.Land;
import cn.keniykey.mc.landterritory.toolkit.Util;
import cn.keniykey.mc.viewlibrary.container.api.EContainerType;
import cn.keniykey.mc.viewlibrary.view.api.AbFrameView;
import cn.keniykey.mc.viewlibrary.view.api.FrameViewBundle;
import cn.keniykey.mc.viewlibrary.view.api.FrameViewIntent;
import cn.keniykey.mc.viewlibrary.view.api.IViewClickHandler;
import cn.keniykey.mc.viewlibrary.view.api.ViewClickReport;

public final class LandSoldListView extends AbLandFrameView{

	protected LandSoldListView(Player player1) {
		super(player1);
	}
	
	private static final String VIEW_NAME = "landlist";
	
	private int page = 1,page_max;
	private List<Land> list_lands;
	
	@Override
	protected void onCreate() {
		super.onCreate();
		this.setViewType(EContainerType.GENERIC_6X9);
		this.list_lands = (this.getIntent().hasExtras() ? 
				this.getIntent().getExtras().getValue("lands", Collections.emptyList()) : Collections.emptyList());
		this.page_max = ((this.list_lands.size() / 45) + 1);
		final boolean layout_all = (this.page_max > 5);
		
		final ViewConfig config_view = ViewsConfiguration.getViewConfig(VIEW_NAME);
		this.setIconClick(49, config_view.getViewItem("back"), AbLandFrameView.CLICK_BACK);
		this.setIconClick(45, config_view.getViewItem("first"), new IViewClickHandler() {
			@Override
			public void onClick(Player player, AbFrameView view, ViewClickReport report) {
				if(LandSoldListView.this.page < 2) return;
				if(!LandSoldListView.this.isMatchClickSpeed()) return;
				LandSoldListView.this.refreshPage(1);
			}
		});
		this.setIconClick((layout_all ? 46 : 47), config_view.getViewItem("last_1"), new IViewClickHandler() {
			@Override
			public void onClick(Player player, AbFrameView view, ViewClickReport report) {
				if(LandSoldListView.this.page < 2) return;
				if(!LandSoldListView.this.isMatchClickSpeed()) return;
				LandSoldListView.this.refreshPage((LandSoldListView.this.page - 1));
			}
		});
		if(layout_all) {
			this.setIconClick(47, config_view.getViewItem("last_5"), new IViewClickHandler() {
				@Override
				public void onClick(Player player, AbFrameView view, ViewClickReport report) {
					if(LandSoldListView.this.page < 6) return;
					if(!LandSoldListView.this.isMatchClickSpeed()) return;
					LandSoldListView.this.refreshPage((LandSoldListView.this.page - 5));
				}
			});
			this.setIconClick(48, config_view.getViewItem("last_10"), new IViewClickHandler() {
				@Override
				public void onClick(Player player, AbFrameView view, ViewClickReport report) {
					if(LandSoldListView.this.page < 11) return;
					if(!LandSoldListView.this.isMatchClickSpeed()) return;
					LandSoldListView.this.refreshPage((LandSoldListView.this.page - 10));
				}
			});
			this.setIconClick(50, config_view.getViewItem("next_10"), new IViewClickHandler() {
				@Override
				public void onClick(Player player, AbFrameView view, ViewClickReport report) {
					final int page_want = (LandSoldListView.this.page + 10);
					if(page_want > LandSoldListView.this.page_max) return;
					if(!LandSoldListView.this.isMatchClickSpeed()) return;
					LandSoldListView.this.refreshPage(page_want);
				}
			});
			this.setIconClick(51, config_view.getViewItem("next_5"), new IViewClickHandler() {
				@Override
				public void onClick(Player player, AbFrameView view, ViewClickReport report) {
					final int page_want = (LandSoldListView.this.page + 5);
					if(page_want > LandSoldListView.this.page_max) return;
					if(!LandSoldListView.this.isMatchClickSpeed()) return;
					LandSoldListView.this.refreshPage(page_want);
				}
			});
		}
		this.setIconClick((layout_all ? 52 : 51), config_view.getViewItem("next_1"), new IViewClickHandler() {
			@Override
			public void onClick(Player player, AbFrameView view, ViewClickReport report) {
				if(LandSoldListView.this.page >= LandSoldListView.this.page_max) return;
				if(!LandSoldListView.this.isMatchClickSpeed()) return;
				LandSoldListView.this.refreshPage((LandSoldListView.this.page + 1));
			}
		});
		this.setIconClick(53, config_view.getViewItem("end"), new IViewClickHandler() {
			@Override
			public void onClick(Player player, AbFrameView view, ViewClickReport report) {
				if(LandSoldListView.this.page >= LandSoldListView.this.page_max) return;
				if(!LandSoldListView.this.isMatchClickSpeed()) return;
				LandSoldListView.this.refreshPage(LandSoldListView.this.page_max);
			}
		});
		this.refreshPage(1);
	}
	
	private void refreshPage(int page1) {
		for(int index = 0; index < 45; index++) this.getContainer().setItemStack(index, null, false);
		this.clearIconsClick(0, 44);
		this.page = page1;
		final IViewClickHandler handler = new IViewClickHandler() {
			@Override
			public void onClick(Player player, AbFrameView view, ViewClickReport report) {
				if(!report.isLeftClick() || !LandSoldListView.this.isMatchClickSpeed()) return;
				final int row = this.getPageIndex(report.getClickRawSlot());
				if(row >= LandSoldListView.this.list_lands.size()) return;
				final Land land = LandSoldListView.this.list_lands.get(row);
				if(land.isDeleted()) return;
				final FrameViewBundle bundle = new FrameViewBundle();
				final FrameViewIntent intent = new FrameViewIntent(LandSoldInfoView.class);
				bundle.putValue("land", land);
				intent.putExtras(bundle);
				view.showView(intent);
			}
			private int getPageIndex(int row) {
				return (((LandSoldListView.this.page - 1) * 45) + row);
			}
		};
		final int begin_flag_index = ((this.page - 1) * 45);
		final ViewConfig config_view = ViewsConfiguration.getViewConfig(VIEW_NAME);
		final ItemStack item_backup = config_view.getViewItem("about");
		for(int index = 0,flag_index = begin_flag_index, flag_max_index = this.list_lands.size();
				flag_index < flag_max_index && index < 45; index++,flag_index++) {
			final Land land = this.list_lands.get(flag_index);
			final ItemStack item = item_backup.clone();
			final ItemMeta meta = item.getItemMeta();
			meta.setDisplayName(Util.setStr(meta.getDisplayName(), LanguageManager.getLang(land.getType().getLang()),
					land.getID(), land.getName()));
			item.setItemMeta(meta);
			this.getContainer().setItemStack(index, item, false);
			this.setIconClick(index, handler);
		}
		this.setTitle(Util.conver(Util.setStr(config_view.getViewTitle(), this.page, this.page_max)));
	}
}